package com.zhang.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.crm.constant.Constants;
import com.zhang.crm.mapper.TPermissionmapper;
import com.zhang.crm.mapper.TUsermapper;
import com.zhang.crm.pojo.TPermission;
import com.zhang.crm.pojo.TUser;
import com.zhang.crm.pojo.req.UserReq;
import com.zhang.crm.service.UserService;
import com.zhang.crm.util.UserUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    @Lazy
    private PasswordEncoder passwordEncoder;
    @Resource
    private TUsermapper tUsermapper;

    @Resource
    private TPermissionmapper tPermissionmapper;

    /**
     * 获取用户信息
     * @param loginAct
     * @return
     */
    @Override
    public UserDetails getUser(String loginAct) {
        TUser tUser = tUsermapper.selectUserByUsername(loginAct);
        if (tUser == null){
            return null;
        }
        //获取用户权限
        List<String> tPermission = tPermissionmapper.selectAuthByUid(tUser.getId());
        tUser.setStringAuthorityList(tPermission);
        return tUser;
    }

    /**
     * 获取所有用户信息
     * @param current
     * @return
     */
    @Override
    public PageInfo<TUser> getUsers(Integer current) {
        PageHelper.startPage(current, Constants.PAGE_DATA_SIZE);
        List<TUser> users = tUsermapper.selectUserByPage();
        PageInfo<TUser> info = new PageInfo<>(users);
        return info;
    }

    /**
     * 通过id获取用户信息
     * @param id
     * @return
     */
    @Override
    public TUser getUserDetail(Integer id) {
        TUser tUser = tUsermapper.selectByPrimaryKey(id);
        return tUser;
    }

    /**
     * 通过id获取用户信息
     * @param tUser
     * @return
     */
    @Override
    @Transactional
    public Integer addUser(TUser tUser) {
        TUser userInfo = UserUtil.getUserInfo();
        //密码加密
        String encodePwd = passwordEncoder.encode(tUser.getLoginPwd());
        tUser.setLoginPwd(encodePwd);
        tUser.setCreateBy(userInfo.getId());
        tUser.setCreateTime(new Date());
        int insert = tUsermapper.insert(tUser);
        if (insert < 0) {
            throw new RuntimeException("插入失败，数据库错误。");
        }
        return insert ;
    }

    @Override
    @Transactional
    public Boolean editUser(UserReq userReq) {
        //不修改密码
        userReq.setLoginPwd(null);
        TUser user = TUser.builder().build();
        BeanUtils.copyProperties(userReq,user);
        user.setEditTime(new Date());
        user.setEditBy(UserUtil.getUserInfo().getId());
        int i = tUsermapper.updateByPrimaryKeySelective(user);
        if (i < 0) {
            throw new RuntimeException("更新失败:" + user);
        }
        return true;
    }

    @Override
    @Transactional
    public Integer delUserById(Integer userId) {
        int i = tUsermapper.deleteByPrimaryKey(userId);
        if (i < 0) {
            throw new RuntimeException("删除失败");
        }
        return i;
    }

    @Override
    @Transactional
    public Integer delUserBatch(List<String> users) {
        Integer result= tUsermapper.deleteUserBatch(users);
        return result;
    }

    /**
     * 获取所有用户的姓名
     * @return
     */
    @Override
    public List<TUser> getUserSelectName() {
        List<TUser> users = tUsermapper.selectUserName();
        return users;
    }


}
